package com.naiterui.ehp.bs.doctor.service.impl;

import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bs.doctor.service.IDoctorSyncService;
import com.naiterui.ehp.bs.doctor.vo.requestparam.DoctorParam;
import com.naiterui.ehp.bs.patient.repository.IApDoctorRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class DoctorSyncServiceImpl implements IDoctorSyncService {

    @Autowired
    private IApDoctorRepository apDoctorRepository;

    // 添加医生信息
    @Transactional(rollbackFor = Exception.class)
    public void saveDoctor(DoctorParam doctorParam){
        // 判断是否存在
        Doctor doctor = apDoctorRepository.findByIdAndPlatform(doctorParam.getDoctorId(), doctorParam.getPlatform());
        if(doctor == null){
            apDoctorRepository.saveDoctor(doctorParam.getDoctorId(), doctorParam.getHeadUrl(), doctorParam.getName(), 0, doctorParam.getHospitalName(), 0,
                    doctorParam.getCustomDepartment(), doctorParam.getTitleId(), doctorParam.getRecommCode(), doctorParam.getExpertise(), doctorParam.getIntroduction(),
                    doctorParam.getPlatform());
        }else{
            apDoctorRepository.updateDoctor(doctorParam.getHeadUrl(), doctorParam.getName(), 0, doctorParam.getHospitalName(), 0,
                    doctorParam.getCustomDepartment(), doctorParam.getTitleId(), doctorParam.getRecommCode(), doctorParam.getExpertise(), doctorParam.getIntroduction(),
                    doctorParam.getDoctorId());
        }
    }

    // 修改医生信息
    @Transactional(rollbackFor = Exception.class)
    public void updateDoctor(DoctorParam doctorParam){
        apDoctorRepository.updateDoctor(doctorParam.getHeadUrl(), doctorParam.getName(), 0, doctorParam.getHospitalName(), 0,
                doctorParam.getCustomDepartment(), doctorParam.getTitleId(), doctorParam.getRecommCode(), doctorParam.getExpertise(), doctorParam.getIntroduction(),
                doctorParam.getDoctorId());
    }
}
